package com.bbcare.department.plat.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.bbcare.comm.DateUtil;
import com.bbcare.comm.SeqUtil;
import com.bbcare.department.plat.entity.Register;
import com.bbcare.department.plat.service.IRegisterService;
import com.bbcare.department.plat.store.ibatis.IRegisterDao;

@Service
public class RegisterService implements IRegisterService {
	
	Logger logger = Logger.getLogger(RegisterService.class); 
	
	@Autowired
	private IRegisterDao registerDao;
	
	/**
	 * 查询科室所有挂号方式
	 * @param arg
	 * @return
	 * @throws Exception 
	 */
    public List<Register> getRegistersByDeptId(String deptId) throws Exception {
        logger.info("in getRegistersByDeptId----arg==" + deptId);
        List<Register> registers =  registerDao.selectByDepId(deptId);
        logger.info("out getRegistersByDeptId----arg==");
        return registers;
    }
    
    /**
     * 查询单个挂号信息
     */
    public Register getRegisterById(String id) throws Exception{
    	logger.info("in getRegisterById" );
    	Register register =  registerDao.selectById(id);
        logger.info("out getRegisterById");
        return register;
    }

	@Override
	public int deleteRegisterByIds(List<String> list) throws Exception {
		logger.info("in deleteRegisterByIds");
        int i =  registerDao.deleteyIds(list);
        logger.info("out deleteRegisterByIds");
        return i;
	}

	@Override
	@Transactional
	public String registerMoidify(List<Register> registers) throws Exception {
		int j = 0;
		String now = DateUtil.getSysDateA();
		List<Register> addList = new ArrayList<Register>();

		for(int i = 0 ;i<registers.size();i++){
			if(StringUtils.isEmpty(registers.get(i).getId())){
				registers.get(i).setId(SeqUtil.getSeqNo());
				registers.get(i).setCreateTime(now);
				addList.add(registers.get(i));
			}else{
				registers.get(i).setUpdateTime(now);
				int n = registerDao.update(registers.get(i));
				j+= n;
			}
		}
		//sql里面批量插入多条
		if(!CollectionUtils.isEmpty(addList)){
			j+= registerDao.save(addList);
		}
		return String.valueOf(j);
	}
}
